﻿var Yxcm = {

    extend: function(bc, sc, o) {
        var f = function() { };
        f.prototype = sc.prototype;
        bc.prototype = new f();
        bc.prototype.constructor = bc;
        bc.superclass = sc.prototype;
        for (var m in o)
            bc.prototype[m] = o[m];
    },

    setScope: function(callback, scope) {
        return function() {
            return callback.apply(scope, arguments);
        };
    },

    namespace: function() {
        var ns, d;
        for (var i = 0; i < arguments.length; i++) {
            ns = window;
            d = arguments[i].split(".");
            for (var j in d)
                ns = ns[d[j]] = ns[d[j]] ? ns[d[j]] : {};
        };
    },

    isObject: function(obj) {
        return (obj && obj.constructor && obj.constructor == Object.prototype.constructor && !obj.nodeName)
            ? true : false;
    },

    isArray: function(arr) {
        return /array/i.test(arr.constructor) ? true : false;
    }
};

Yxcm.Base = function() {
    if (Yxcm.isObject(arguments[0])) {
        var config = arguments[0];
        Yxcm.Base.superclass.init.call(this);
        this.initialize(config);
    } else {
        if (arguments.length > 0) {
            Yxcm.Base.superclass.init.apply(this, arguments);
        } else {
            Yxcm.Base.superclass.init.call(this);
        }
    }
};

Yxcm.extend(Yxcm.Base, jQuery, {
    initialize: function(config) { }
});

Yxcm.Event = function() {
    this.eventHandlers = {};
    Yxcm.Event.superclass.constructor.apply(this, arguments);
};

Yxcm.extend(Yxcm.Event, Yxcm.Base, {

    registerEvent: function(event, handler) {
        var keys = event.indexOf('.') == -1 ? [event] : event.split('.');
        if (!this.eventHandlers[keys[0]]) {
            this.eventHandlers[keys[0]] = new Array();
        }

        var eventName = keys.length == 2 ? keys[1] : '_default';
        this.eventHandlers[keys[0]].push({ name: eventName, callback: handler });
    },

    notifyEvent: function(event, arg) {
        if (this.eventHandlers[event]) {
            jQuery.each(this.eventHandlers[event], function(index, item) {
                item.callback(arg);
            });
        }
    },

    removeEvent: function(event) {
        var keys = event.indexOf('.') == -1 ? [event] : event.split('.');
        if (this.eventHandlers[keys[0]]) {
            if (keys.length == 2) {
                jQuery.each(this.eventHandlers[event], function(index, item) {
                    item = null;
                    return false;
                });
            } else {
                this.eventHandlers[keys[0]] = null;
            }
        }
    }
});

Yxcm.Util = {
    getMousePoint: function(event) {
        if (jQuery.browser.msie) {
            return { x: event.offsetX, y: event.offsetY };
        } else {
            return { x: event.layerX, y: event.layerY };
        }
    }
};

var ajaxLoadingImage = '/loading.gif';
Yxcm.Ajax = {

    showLoading: function() {
        if (jQuery('#ajaxLoading').length == 0) {
            jQuery('body').append('<div id="ajaxLoading" style="position:absolute;left:0;top:0;width:100%;"><img src="' + ajaxLoadingImage + '"/></div>');
        }
        var bodyH = jQuery(document).height();
        jQuery('#ajaxLoading').height(bodyH).show();
    },

    closeLoading: function() {
        jQuery('#ajaxLoading').hide();
    },

    failStatusHandler: function() {
        alert('请求失败');
    },

    processResult: function(setting, data) {
        if (setting.dataType == 'json') {
            var n = data.Result + 'Handler';
            if (setting[n]) {
                setting[n](data);
            } else if (setting.resultHandler) {
                setting.resultHandler(data);
            } else if (Yxcm.Ajax[n]) {
                Yxcm.Ajax[n](data);
            }
        } else {
            if (setting.resultHandler) {
                setting.resultHandler(data);
            } else if (setting.fillTo) {
                jQuery(container).html(data);
            } else if (setting.appendTo) {
                jQuery(container).append(data);
            }
        }
    },

    loadResult: function(setting) {
        var customSuccessHandler = setting.successHandler;
        var config = setting;
        config.dataType = 'json';
        config.successHandler = function(data) {
            config.successHandler = customSuccessHandler;
            Yxcm.Ajax.processResult(config, data);
        };
        Yxcm.Ajax.load(config);
    },   

    load: function(setting) {
        var config = jQuery.extend({}, { loading: true, dataType: 'html' }, setting);
        if (config.loading) {
            Yxcm.Ajax.showLoading();
        }
        jQuery.ajax({
            url: setting.url,
            dataType: config.dataType,
            data: config.data,
            success: function(data) {
                Yxcm.Ajax.processResult(config, data);
                Yxcm.Ajax.closeLoading();
            }
        });
    },

    submit: function(setting) {
        var config = jQuery.extend({}, { loading: true, dataType: 'json' }, setting);
        if (setting.loading) {
            Yxcm.Ajax.showLoading();
        }
        jQuery('#' + setting.form).ajaxSubmit({
            success: function(responseText, statusText, xhr, $form) {
                var data = responseText;
                if (config.dataType == 'json') {
                    data = eval('(' + data + ')');
                }
                Yxcm.Ajax.processResult(config, data);
                Yxcm.Ajax.closeLoading();
            }
        });
    }
};